package com.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.pojo.EpidemicSpecialThings;
import com.pojo.Result;
import com.service.EpidemicSpecialThingsServie;
import com.service.OssService;
import com.service.impl.OssServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLDecoder;
import java.util.List;

/**
 * @author ZhaoJiaDong
 * @version 2020.1
 * @date 2022/11/25 0025 11:30
 */
@RestController
@RequestMapping("/epidemic_special_things")
@CrossOrigin
public class EpidemicSpecialThingsController {
    private String url="";
    @Autowired
    private EpidemicSpecialThingsServie epidemicSpecialThingsServie;
    @Autowired
    private OssService ossService;
    //查询特殊事物的所有表单
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        System.out.println("根据页数查询方法运行了");
        List<EpidemicSpecialThings> list = epidemicSpecialThingsServie.findByPage(currentPage,pageSize);
        PageInfo<EpidemicSpecialThings> pageInfo = new PageInfo<>(list);
        Result result = new Result();
        if(list!=null){
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }


    //添加特殊事物联系信息
    @PostMapping
    public Result add(@RequestBody EpidemicSpecialThings epidemicsSpecialThings){
        System.out.println("add方法运行了");
        boolean flag=epidemicSpecialThingsServie.add(epidemicsSpecialThings);
        Result result = new Result();
        if(flag){
            result.setMsg("添加成功");
            result.setCode(200);
        }else{
            result.setMsg("添加失败");
            result.setCode(500);
        }
        return result;
    }

    //删除特殊事物信息
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        System.out.println("删除的方法运行了");
        boolean flag = epidemicSpecialThingsServie.deleteById(id);
        Result result = new Result();
        if(flag){
            result.setMsg("删除成功");
            result.setCode(200);
        }else{
            result.setMsg("删除失败");
            result.setCode(500);
        }
        return result;
    }

    //改变图片因为用putMapping接收不到img参数，所以用最普通的
    @RequestMapping("/{id}")
    public Result updateImgById(@PathVariable Integer id){
//        //因为接收到的imgurl最后带=，所以去掉
//        StringBuffer buffer = new StringBuffer(img);
//        //删除指定位置的字符，并返回本身
//        buffer.deleteCharAt(img.length() - 1);//删除最后位的元素
//        String param = buffer.toString();
//        System.out.println("更改图片的方法运行了"+img);
//        String decodeImg = URLDecoder.decode(param);
//        System.out.println("解码后"+decodeImg);

        boolean flag = epidemicSpecialThingsServie.updateImgById(id,this.url);
        Result result = new Result();
        if(flag){
            result.setMsg("修改成功");
            result.setCode(200);
        }else{
            result.setMsg("修改失败");
            result.setCode(500);
        }
        return result;
    }

    @PostMapping("/getImageUrl")
    public R uploadOssFile(MultipartFile file){
        System.out.println("file="+file.getOriginalFilename());
        this.url = ossService.uploadFileAvatar(file);
        System.out.println("实打实大卧底啊欧弟脑袋那我ID加我就打我基底哦"+url);
        return R.ok(url).setCode(20000);
    }


    @PutMapping
    public Result update(@RequestBody EpidemicSpecialThings epidemicSpecialThings){
        System.out.println("编辑的方法运行了");
        boolean flag = epidemicSpecialThingsServie.update(epidemicSpecialThings);
        Result result = new Result();
        if(flag){
            result.setMsg("修改成功");
            result.setCode(200);
        }else{
            result.setMsg("修改失败");
            result.setCode(500);
        }
        return result;
    }
}
